import { Module, Global } from "@nestjs/common";
import * as path from "path";
import { I18nModule, HeaderResolver, I18nJsonLoader } from "nestjs-i18n";
import { ConfigService } from "@nestjs/config";
import { ENUM_MESSAGE_LANGUAGE } from "./message.constant";
import { MessageService } from "./service/message.service";
import { MessageEnumController } from "./controller/message.enum.controller";

@Global()
@Module({
  providers: [MessageService],
  exports: [MessageService],
  imports: [
    I18nModule.forRootAsync({
      useFactory: (configService: ConfigService) => ({
        fallbackLanguage: configService.get<string>("app.language"),
        fallbacks: Object.values(ENUM_MESSAGE_LANGUAGE).reduce((a, v) => ({ ...a, [`${v}-*`]: v }), {}),
        loaderOptions: {
          path: path.join(__dirname, "/languages/"),
          watch: true,
        },
      }),
      loader: I18nJsonLoader,
      inject: [ConfigService],
      resolvers: [new HeaderResolver(["x-custom-lang"])],
    }),
  ],
  controllers: [MessageEnumController],
})
export class MessageModule {}
